package drds.binlog.event;

import drds.binlog.LogByteBlock;

/**
 * Create_file_log_event.
 */
public final class CreateFileLogEvent extends LoadLogEvent
{

    /* CF = "Create File" */
    public static final int CF_FILE_ID_OFFSET = 0;
    public static final int CF_DATA_OFFSET = FormatDescriptionLogEvent.CREATE_FILE_HEADER_LEN;
    protected LogByteBlock blockBuf;
    protected int blockLen;
    protected long fileId;
    protected boolean initedFromOld;

    public CreateFileLogEvent(LogHeader logHeader, LogByteBlock logByteBlock, FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        super(logHeader, logByteBlock, formatDescriptionLogEvent);

        final int headerLen = formatDescriptionLogEvent.commonHeaderLength;
        final int loadHeaderLen = formatDescriptionLogEvent.postHeaderLengths[LOAD_EVENT - 1];
        final int createFileHeaderLen = formatDescriptionLogEvent.postHeaderLengths[CREATE_FILE_EVENT - 1];

        copyLogEvent(logByteBlock,
                ((logHeader.type == LOAD_EVENT) ? (loadHeaderLen + headerLen) : (headerLen + loadHeaderLen + createFileHeaderLen)),
                formatDescriptionLogEvent);

        if (formatDescriptionLogEvent.binlogVersion != 1)
        {
            fileId = logByteBlock.getUint32(headerLen + loadHeaderLen + CF_FILE_ID_OFFSET);
            /*
             * Note that it's ok to use get_data_size() below, because it is
             * computed with values we have already read from this event
             * (because we called copy_log_event()); we are not using slave's
             * format info to decode master's format, we are really using
             * master's format info. Anyway, both formats should be identical
             * (except the common_header_len) as these Load events are not
             * changed between 4.0 and 5.0 (as logging of LOAD DATA INFILE does
             * not use Load_log_event in 5.0).
             */
            blockLen = logByteBlock.limit() - logByteBlock.index$originIndex();
            blockBuf = logByteBlock.duplicateBasedOnIndex(blockLen);
        } else
        {
            initedFromOld = true;
        }
    }

    public final long getFileId()
    {
        return fileId;
    }

    public final LogByteBlock getBuffer()
    {
        return blockBuf;
    }

    public final byte[] getData()
    {
        return blockBuf.getData();
    }
}
